package com.example.demo.util.imgsimilar;

import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Size;
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;

import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;
import static org.bytedeco.opencv.global.opencv_imgproc.*;

public class ImagePreprocessor {
    public static Mat preprocessImage(String imagePath) {
        // 读取图像
        Mat image = imread(imagePath, opencv_imgcodecs.IMREAD_COLOR);
        if (image.empty()) {
            System.err.println("无法读取图像: " + imagePath);
            return null;
        }

        // 灰度化
        Mat grayImage = new Mat();
        cvtColor(image, grayImage, COLOR_BGR2GRAY);

        // 高斯模糊
        Mat blurredImage = new Mat();
        GaussianBlur(grayImage, blurredImage, new Size(5, 5), 0);

        // 直方图均衡化
        Mat equalizedImage = new Mat();
        equalizeHist(blurredImage, equalizedImage);

        return equalizedImage;
    }
}